<?php
$public = true;
if (!isset($common)) require_once 'include/common.php';
?>

<?php include 'ui/header.php'; ?>

<title>the tims</title>

<?php include 'ui/body.php'; ?>
<html>
  <head>
    <title>Sign up</title>
      </head>
  <body>
<?php
function form()
{
    ?>
        <br />
        <br />
        
    
    <form action="<?php $PHP_SELF;?>" method="post">
    <p><font color="white" size=4 face="arial">Usuario:</font> <input type="text" name="username" value="" /></p>
    <p><font color="white" size=4 face="arial">Contraseña:</font> <input type="password" name="password" value="" /></p>
    <p><font color="white"size=4 face="arial">Nombre ciudad:</font> <input type="text" name="namecity" value="" /></p>
    <p><font color="white" size=4 face="arial">E-mail:</font> <input type="text" name="mail" value="" /></p>
	<p><font color="white" size=4 face="arial">Idioma:</font> <select name="lang">
	  <option value="es">Español
	  <option value="en">Inglés
	  <option value="eu">Euskera
	  </select></p>  
	      <br />
	   

	  
    <input type="submit" value="OK" />         <INPUT TYPE="BUTTON" VALUE="Login" ONCLICK="window.location.href='signin.php'"> 

    
    </form>
        
    <?
}

        if(isset($_COOKIE["username"]))
        // Section for logged users
        {
            echo "I know, you are ".$_COOKIE["username"].".";
            echo '<a href="signout.php">sign out</a>';
        }
        // Section for unlogged users
        else
        {
            if(isset($_REQUEST['username']) && !empty($_REQUEST['username'])
            		&& isset($_REQUEST['password']) && !empty($_REQUEST['password'])
            		&& isset($_REQUEST['namecity']) && !empty($_REQUEST['namecity'])
            		&& isset($_REQUEST['mail']) && !empty($_REQUEST['mail'])
            		&& isset($_REQUEST['lang']) && !empty($_REQUEST['lang']))
            // Section for logging process
            {
                $username = trim($_REQUEST['username']); //TODO comprobar que el nombre de usuario no está registrado.
                $password = trim($_REQUEST['password']);
                $namecity = trim($_REQUEST['namecity']);
                $mail = trim($_REQUEST['mail']);
                $lang = trim($_REQUEST['lang']);
                
                // Obtain the user's city's coord:
                require_once 'include/functions.php';
                $coord = getNewCityCoord(); //TODO comprobar que queden casillas libres
                
                //Connect to DB
               	require 'include/dbconnect.php';
                                          
            	// Insert new user in the DB
                if( !mysql_query("INSERT INTO users (username, password, mail, lang)
				VALUES ('".$username."', '".$password."', '".$mail."', '".$lang."')" ) )
                {
                	die('Error: '. mysql_error());
                }
                
            	$iduser = mysql_insert_id($con);
                if( !$iduser )
                {
                	die('Error: '. mysql_error());
                }
                              
            	// Insert the new city
           		if( !mysql_query("INSERT INTO cities (namecity, iduser, coordx, coordy,box0,box0lvl,box1,box1lvl,box2,box2lvl,box3,box3lvl,box4,box4lvl,box5,box5lvl,
				box6,box6lvl,box7,box7lvl,box8,box8lvl,box9,box9lvl,box10,box10lvl,box11,box11lvl,box12,box12lvl,box13,box13lvl,box14,box14lvl,box15,box15lvl,
           		tropas0,tropas1,tropas2,tropas3,tropas4,grano,hierro,oro,madera,last_resources_update)
				VALUES ('".$namecity."', '".$iduser."', '".$coord->getCoordX()."', '".$coord->getCoordY()."','-1','-1','-1','-1','-1','-1','-1','-1','-1',
				'-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1',
				'-1','-1','-1','$init_tropas0','$init_tropas1','$init_tropas2','$init_tropas3','".$init_tropas4."','".$init_grano."','".$init_hierro."','".$init_oro."','".$init_madera."','".time()."')") )
                {
                	die('Error: '. mysql_error());
                }
                
            	
                
                require 'include/dbdisconnect.php';
                
                // Create the session and cookie
                require 'include/class/usersession.php';
                usersession::loadSession($username);
                
                // Send an email
                
				$destinatario = $mail;
				$asunto = "Welcome to The Tims!";
				$cuerpo = '
				<html>
				<head>
				   <title>Welcome to The Tims!</title>
				</head>
				<body>
				<h1>You have been signed up to The Tims!</h1>
				<p>
				You can now start playing. Your username is '.$username.' and your password is '.$password.'.
				</p>
				</body>
				</html>
				';
				
				//para el envío en formato HTML
				$headers = "MIME-Version: 1.0\r\n";
				$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
				
				//dirección del remitente
				$headers .= "From: The Tims Team\r\n";
				
				//dirección de respuesta, si queremos que sea distinta que la del remitente
				//$headers .= "Reply-To: mariano@desarrolloweb.com\r\n";
				
				//ruta del mensaje desde origen a destino
				//$headers .= "Return-path: holahola@desarrolloweb.com\r\n";
				
				//direcciones que recibián copia
				//$headers .= "Cc: maria@desarrolloweb.com\r\n";
				
				//direcciones que recibirán copia oculta
				//$headers .= "Bcc: pepe@pepe.com,juan@juan.com\r\n";
				
				mail($destinatario,$asunto,$cuerpo,$headers); // It works once online                
                
                
                ?>
                <?php p('welcome');?>
                <br />
                <p><a href="signout.php">sign out</a></p>
				<p><a href="index.php">home</a></p>
				<?php
            }
            else
			// Section for signin up
            {
                form();
            }
        }
?>
  </body>
</html>

<?php include 'ui/end.php'; ?>


